package me.numark;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.HashMap;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;
import me.numark.model.News;
import me.numark.net.BaseResponse;
import me.numark.net.GsonRequest;
import me.numark.net.NetworkConstants;
import me.numark.net.NetworkHelper;
import me.numark.utils.Constants;
import me.numark.utils.Settings;

/**
 * Created by tian on 14/11/20:上午9:14.
 */
public class PreRecommendActivity extends BaseActivity implements View.OnClickListener {

    @InjectView(R.id.submit_button)
    TextView rightButton;

    @InjectView(R.id.edit)
    EditText editText;

    @InjectView(R.id.background)
    View background;

    @InjectView(R.id.recommend_layout)
    View content;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pre_recommend);
        ButterKnife.inject(this);
        rightButton.setOnClickListener(this);
        rightButton.setEnabled(false);
        background.setOnClickListener(this);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s != null && s.length() > 0) {
                    rightButton.setEnabled(true);
                } else {
                    rightButton.setEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.submit_button:
                handleRecommend();
                break;
            case R.id.background:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void finish() {
        background.setBackgroundColor(getResources().getColor(android.R.color.transparent));
        super.finish();
        overridePendingTransition(R.anim.push_out_to_bottom, R.anim.push_out_to_bottom);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.push_in_from_bottom);
        content.startAnimation(animation);
        AlphaAnimation animation1 = new AlphaAnimation(0.0f, 1.0f);
        animation1.setDuration(200);
        animation1.setFillAfter(true);
        background.startAnimation(animation1);
    }

    private void handleRecommend() {
        String url = String.format(NetworkConstants.NEW_TWEET_URL, Settings.getUid());
        Map<String, String> body = new HashMap<String, String>();
        body.put("url", editText.getText().toString());
        GsonRequest<NewTweetResponse> request = new GsonRequest<NewTweetResponse>(url,
                Request.Method.POST,
                NewTweetResponse.class, true, body, new Response.Listener<NewTweetResponse>() {
            @Override
            public void onResponse(NewTweetResponse response) {
                dismissProgress();
                if (response.ret) {
                    Toast.makeText(PreRecommendActivity.this, R.string.recommend_success,
                            Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(PreRecommendActivity.this, RecommendActivity.class);
                    intent.putExtra(Constants.NEWS_EXTRA, response.news.toString());
                    startActivityForResult(intent, 0);
                } else {
                    Toast.makeText(PreRecommendActivity.this, response.msg, Toast.LENGTH_SHORT)
                            .show();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                dismissProgress();
                Toast.makeText(PreRecommendActivity.this, R.string.generic_network_error,
                        Toast.LENGTH_SHORT).show();
            }
        });
        NetworkHelper.getInstance().addToRequestQueue(request);
        NetworkHelper.getInstance().getRequestQueue().start();
        showProgress(R.string.submitting);
    }

    @Override
    public void onBackPressed() {
        handleFinish();
    }

    private void handleFinish() {
        if (TextUtils.isEmpty(editText.getText().toString())) {
            finish();
        } else {
            new AlertDialog.Builder(this).setTitle(R.string.confirm_discard)
                    .setPositiveButton(android.R.string.ok,
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    setResult(RESULT_CANCELED);
                                    finish();
                                }
                            }).setNegativeButton(android.R.string.cancel, null).show();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        finish();
    }

    public static class NewTweetResponse extends BaseResponse {

        public News news;
    }
}
